<?php
namespace Home\Controller;
use Utuo\ForegroundController;

class SiteController extends ForegroundController {
	
	public function _init() {
		layout(false);
		$this->setModelName();
	}
	
	// home
    public function indexAction() {
    	$pageTitle=M('site')->where("id=".$this->sid." and status=1")->getField("name");
    	$this->assign("pageTitle",$pageTitle);
    	$this->display();
    }
    
    // get category detail
    private function getCategory() {
    	$this->catId = $id = I('id', 0, 'intval');
    	$map['cid'] = $this->sid;
    	$map['status'] = 1;
    	$map['is_deleted'] = 0;
    	$category = M('ArticleCategory')->where($map)->find($id);
    	$temparr=$this->getTopCategory($category['id']);
    	$category['topCategory']=$temparr['id'];
    	$this->assign('category', $category);
    	$this->assign('cat_id', $id);
    	return $category;
    }
    //获取顶级目录
    private function getTopCategory($id){
    	
    	$data=M('ArticleCategory')->where("id=%d",$id)->field("id,pid")->find();
    	
    	if($data['pid']!=0)
    	{
    		$data=$this->getTopCategory($data['pid']);
    	}
   
    	return $data;
    }
    // 文章单页栏目
    public function pageAction() {
    	$category = $this->getCategory();
    	$this->doDisplay( $category, 'page');
    }
    
    // 文章栏目列表
    public function cateAction() {
    	$category = $this->getCategory();
    	$this->doDisplay( $category, 'cate' );
    }
    
    private function doDisplay( $category, $tplType ) {
    	$siteThemeTpl = D("SiteThemeTpl", "Logic");
    	$tpl = $siteThemeTpl->getCategoryTpl( unserialize($category['tpl_config']), $tplType );
    	
        $this->display($tpl?$tpl:'');
    }
    /*public function moreAction(){
    $this->doDisplay( );
    }
    public function getMoreAction($sid,$pid){
    	$condations=array(
    		'cid'=>$sid,
    		'pid'=>$pid,
    		'status'=>1,
    		'is_deleted'=>0			
    	);
    	$articleCategory=M('ArticleCategory')->where($condations)->page($this->getPage(),3)->select();
    	foreach($articleCategory as &$v){
    		if($v['type'] == 'page') {
    			$v['url'] = get_url($sid, 'Site', 'page', 'id=' . $v['id']);
    		} elseif($v['type']=='list'){
    			if($v['has_child']){
    				$v['url'] = get_url($sid, 'Site', 'cate', 'id=' . $v['id']);
    			} else {
    				$controllerName = "Article";
    				empty( $v['content_model'] ) or $controllerName = $v['content_model'];
    				$v['url'] = get_url($sid, $controllerName, 'list', 'id=' . $v['id']);
    			}
    		}
    	}
    	$this->ajaxReturn($articleCategory);
    }
    // 文章列表
    public function listAction() {
    	$this->getCategory();
    	$this->display();
    }
    
    // 更多文章
    public function moreAction() {
  
     	$this->getCategory();
    	$list = $this->lists($this->catId, true, $this->getPageSize(), $this->getPage());
    	echo M()->getLastSql();
    	$totalPages = ceil($list['count'] / $this->getPageSize()); //总页数
    	// 显示更多文章数据
    	$data = array();
    	if($this->getPage() <= $totalPages) {
    		$data = $list['data'];
    		foreach ($data as &$v) $v['url'] = get_url($this->sid, 'Article', 'show', "id={$v['id']}", $v['id']);
    	}
    	dump($data);
    	$this->ajaxSuccess('', $data);
    }
    
    private function lists($cateId = 0, $paginate = false, $pageSize = 10, $p = 1) {
    	$condition = array(
    			"cid" => $this->sid,
    			"is_deleted" => 0,
    			"status" => 1,
    			"cat_id" => $cateId,
    	);
    	$m = M("Article")->where($condition);
    	$new_m = clone $m;
    	// 分页处理
    	if($paginate) {
    		$m->limit($this->getPageSize())->page($this->getPage());
    		$contentCount = $new_m->count();
    	}
    	$contentLists = $m->order('update_time desc')->select();
    	return array('count' => $contentCount, 'data' => $contentLists);
    }*/
    
}